package com.kantboot.api.phone.util;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.aliyun.auth.credentials.provider.StaticCredentialProvider;
import com.aliyun.sdk.service.dysmsapi20170525.AsyncClient;
import com.aliyun.sdk.service.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.sdk.service.dysmsapi20170525.models.SendSmsResponse;
import com.google.gson.Gson;
import com.kantboot.api.phone.domain.dto.SmsOfAliCloudSendDTO;
import com.kantboot.util.common.exception.BaseException;
import darabonba.core.client.ClientOverrideConfiguration;
import lombok.SneakyThrows;

import java.util.Map;
import java.util.concurrent.CompletableFuture;

public class SmsUtil {

    /**
     * 七牛云发送短信
     */
    public static void sendOfQiniuCloud() {

    }

    /**
     * 发送短信
     * 北京深智恒际科技有限公司
     * <a href="https://market.aliyun.com/apimarket/detail/cmapi00037170?spm=5176.2020520132.101.21.77fb7218HWiC1I">查看</a>
     * @param phone 手机号
     * @param content 内容
     * @param templateId 模板id
     * @param appcode appcode
     */
    public static void sendOfAliCloudOfDfsns(String phone, String content, String templateId, String appcode) {
        Map<String,Object> map = Map.of("phone_number",phone,"content",content,"template_id",templateId);
        String authorization = HttpUtil
                .createPost("https://dfsns.market.alicloudapi.com/data/send_sms")
                .contentType("application/x-www-form-urlencoded;charset=utf-8")
                .header("Content-Type", "application/x-www-form-urlencoded;charset=utf-8")
                .header("Authorization", "APPCODE "+appcode)
                .form(map)
                .execute()
                .body();
        if (authorization.contains("Error request, response status: 403")) {
             throw BaseException.of("smsNotPay","短信服务暂时停止");
        }
        JSONObject jsonObject = JSON.parseObject(authorization);
        if ("RATE_LIMIT".equals(jsonObject.get("status"))) {
            throw BaseException.of("smsRateLimit","此号码短信发送频率过高");
        }

    }

    /**
     * 阿里云发送短信
     */
    @SneakyThrows
    public static void sendOfAliCloud(SmsOfAliCloudSendDTO dto){

        StaticCredentialProvider aliCloudCredentialProvider = dto.getAliCloudCredentialProvider();

        // 配置Client
        AsyncClient client = AsyncClient.builder()
                .region("cn-shanghai") // Region ID
                .credentialsProvider(aliCloudCredentialProvider)
                .overrideConfiguration(
                        ClientOverrideConfiguration.create()
                                .setEndpointOverride("dysmsapi.aliyuncs.com")
                )
                .build();

        SendSmsRequest sendSmsRequest = SendSmsRequest.builder()
                .signName(dto.getSignName())
                .templateCode(dto.getTemplateCode())
                .phoneNumbers(dto.getPhoneNumbers())
                .templateParam(new Gson().toJson(dto.getTemplateParam()))
                .build();

        // 异步获取API请求的返回值
        CompletableFuture<SendSmsResponse> response = client.sendSms(sendSmsRequest);
        // 同步获取API请求的返回值
        SendSmsResponse resp = response.get();
        System.err.println(new Gson().toJson(resp));
        client.close();

    }


}
